package org.fireflow2.designer.eclipse.figures;


import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.fireflow.pdl.fpdl20.diagram.basic.Circle;
import org.fireflow.pdl.fpdl20.process.StartNode;
import org.fireflow.pdl.fpdl20.process.features.Feature;
import org.fireflow.pdl.fpdl20.process.features.startnode.CatchCompensationFeature;
import org.fireflow.pdl.fpdl20.process.features.startnode.CatchFaultFeature;
import org.fireflow.pdl.fpdl20.process.features.startnode.JmsMessageStartFeature;
import org.fireflow.pdl.fpdl20.process.features.startnode.NormalStartFeature;
import org.fireflow.pdl.fpdl20.process.features.startnode.TimerStartFeature;
import org.fireflow.pdl.fpdl20.process.features.startnode.WebserviceStartFeature;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.basic.StartNodeWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;


public class StartNodeFigure extends Label implements CommonFigureInterface{
	StartNodeWrapper startNodeWrapper = null;
	public static final int IconTextGap = 5;
	public static final int MaxTextHeight = 20;
	public static final int MaxTextWidth = 50;
	
	static Map<String,Image> imageFileMap = new HashMap<String,Image>();
	{
		ImageDescriptor imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/empty_start_event.png");
		imageFileMap.put(NormalStartFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/compensation_start_event.png");
		imageFileMap.put(CatchCompensationFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/error_start_event.png");
		imageFileMap.put(CatchFaultFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/timer_start_event.png");
		imageFileMap.put(TimerStartFeature.class.getName(),  imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/message_start_event.png");
		imageFileMap.put(WebserviceStartFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class,  "resources/obj32/message_start_event.png");
		imageFileMap.put(JmsMessageStartFeature.class.getName(),imgDesc.createImage());
	}
	
	
	protected boolean useLocalCoordinates() {
		return true;
	}
	
	/**
	 * 相对位置
	 */
	public Rectangle getBox(){
		Rectangle rectangle = new Rectangle(this.getIconBounds());
		return rectangle;
	}

	public Rectangle getSelectionHandleBounds(){
		Rectangle rectangle = getBox();
		this.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	
	public StartNodeFigure(StartNodeWrapper wrapper){
		startNodeWrapper = wrapper;

		Circle theCircleShape = (Circle)startNodeWrapper.getDiagramElement().getShape();
		
		this.setOpaque(false);
		
		this.setText(getDisplayName());
		this.setFont(Util4Eclipse.getFont(theCircleShape.getLabel()));
		this.setTextPlacement(PositionConstants.SOUTH);
		this.setTextAlignment(CENTER);
		this.setIconAlignment(CENTER);
		this.setIconTextGap(IconTextGap);
		this.setIcon(imageFileMap.get(NormalStartFeature.class.getName()));

	}
	

	
	public String getDisplayName(){
		Circle theCircleShape = (Circle)startNodeWrapper.getDiagramElement().getShape();
		String displayName = startNodeWrapper.getDisplayName();

		if (!org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT.equals(theCircleShape.getTitle())){
			displayName = theCircleShape.getTitle();
		}
		return displayName;
	}
	
	public void refreshFigure(){
		Circle theCircleShape = (Circle)startNodeWrapper.getDiagramElement().getShape();
		StartNode theStartNode = (StartNode)startNodeWrapper.getWorkflowModelElement();

		this.setText(getDisplayName());
		this.setFont(Util4Eclipse.getFont(theCircleShape.getLabel()));
		

		if (theStartNode!=null){
			Feature f = theStartNode.getFeature();
			Image image = imageFileMap.get(NormalStartFeature.class.getName());
			if (f==null || f instanceof NormalStartFeature){
				image = imageFileMap.get(NormalStartFeature.class.getName());
			}
			else if (f instanceof CatchCompensationFeature){
				image = imageFileMap.get(CatchCompensationFeature.class.getName());
			}
			else if (f instanceof CatchFaultFeature){
				image = imageFileMap.get(CatchFaultFeature.class.getName());
			}
			else if (f instanceof TimerStartFeature){
				image = imageFileMap.get(TimerStartFeature.class.getName());
			}
			else if (f instanceof WebserviceStartFeature){
				image = imageFileMap.get(WebserviceStartFeature.class.getName());
			}
			else if (f instanceof JmsMessageStartFeature){
				image = imageFileMap.get(JmsMessageStartFeature.class.getName());
			}
			if (image!=null){
				this.setIcon(image);
			}
		}
		
		repaint();
	}

}
